<template>
    <div>
        <Scroll height="780">
            <div style="font-size: 20px;color: #0d1222;margin: 10px">All Account List</div>
            <div v-for="(item,index) in accountList" v-bind:key="item.agentCode">
                <div v-if="item.checked"
                     style="background:#24d412;color:white;margin: 10px;border-radius: 6px;padding: 10px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);display: flex;flex-direction: row;justify-content: space-between"
                     @click="selectAccount(item.agentCode,item.account,index,item.merchantCode)">
                    <div>
                        <p>机构：{{item.agentCode}}</p>
                        <p>账户：{{item.account}}</p>
                        <p>密码：{{item.password}}</p>
                    </div>
                    <div style="margin-top: 20px">
                        <p v-if="item.status === 0" style="color: black">未登录</p>
                        <p v-else-if="item.status === 1">已登录</p>
                        <p v-else style="color: red">Token失效</p>
                    </div>

                </div>
                <div v-else
                     style="background:white;margin: 10px;border-radius: 6px;padding: 10px;display: flex;flex-direction: row;justify-content: space-between"
                     @click="selectAccount(item.agentCode,item.account,index,item.merchantCode)">
                    <div>
                        <p>机构：{{item.agentCode}}</p>
                        <p>账户：{{item.account}}</p>
                        <p>密码：{{item.password}}</p>
                    </div>
                    <div style="margin-top: 20px">
                        <p v-if="item.status === 0" style="color: coral">未登录</p>
                        <p v-else-if="item.status === 1">已登录</p>
                        <p v-else style="color: red">Token失效</p>
                    </div>
                </div>
            </div>
        </Scroll>


    </div>
</template>
<script>
    const {ipcRenderer} = window.require("electron");
    export default {
        name: "Right",
        data() {
            return {
                accountList: [],
                interval: null,
                currentAccount: '',
                currentAgentCode: '',
                currentMerchantCode: '',
            }
        },
        methods: {
            getAccountList() {
                this.$api.getAccountList().then(res => {
                    this.accountList = res.list
                    if (this.currentAccount === '') {
                        this.currentAccount = this.accountList[0].account
                        this.currentAgentCode = this.accountList[0].agentCode
                        this.currentMerchantCode = this.accountList[0].merchantCode
                        this.accountList[0].checked = true;
                        ipcRenderer.send("initWindow", this.accountList[0].url)
                    } else {
                        for (let i = 0; i < this.accountList.length; i++) {
                            if (this.accountList[i].account === this.currentAccount && this.accountList[i].merchantCode === this.currentMerchantCode) {
                                this.accountList[i].checked = true;
                            }
                        }
                    }
                })
            },

            getAddToken(arg) {
                var token = "";
                if (this.currentMerchantCode === "cashpocket" || this.currentMerchantCode === "plumpwallet" ||
                    this.currentMerchantCode === "peonycashtreeloan" || this.currentMerchantCode === "loannow") {
                    token = arg.adminToken
                } else if (this.currentMerchantCode === "income") {
                    token = arg.token
                } else if (this.currentMerchantCode === "cashfish" || this.currentMerchantCode === "credexpress") {
                    token = arg.authorization
                } else if (this.currentMerchantCode === "cbcash" || this.currentMerchantCode === "okloan" || this.currentMerchantCode === "lendking") {
                    token = arg.cookie
                }
                if (token !== undefined) {
                    this.$api.getAddToken({
                        account: this.currentAccount,
                        merchantCode: this.currentMerchantCode,
                        token: token,
                        tm: arg.tm,
                    }).then(() => {
                        this.getAccountList()
                    })
                }

            },
            getInit() {
                this.$api.getInit().then(() => {
                })
            },
            selectAccount(agentCode, account, index, code) {
                if (this.currentAgentCode === agentCode) {
                    return
                }
                this.currentAgentCode = agentCode
                this.currentAccount = account
                this.currentMerchantCode = code
                for (let i = 0; i < this.accountList.length; i++) {
                    this.accountList[i].checked = false;
                }
                this.accountList[index].checked = true;
                ipcRenderer.send("createNewWindow", this.accountList[index].url)
            }

        },
        mounted() {
            ipcRenderer.on('token-message', (event, arg) => {
                this.getAddToken(arg)
            });
            this.getAccountList()
            // this.interval = setInterval(()=>{
            //     this.getAccountList()
            // },30000);
        },
        created() {
            this.getInit()
        },
        destroyed() {
            clearInterval(this.interval)
        }
    }
</script>